﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace _201206QuanLyBanHang.ChucNang
{
    class BLL_DMKhachHang
    {
        
        DataConn da;
        public BLL_DMKhachHang()
        {
            da = new DataConn();
        }
        public DataSet getDMKhachHang(string strsql)
        {
            return da.ExectueQueryDataset(strsql, CommandType.Text, null);
        }
        public DataSet laysanpham(string strsql)
        {
            return da.ExectueQueryDataset(strsql, CommandType.Text, null);
        }
        public bool insertDMKhachHang(ref string err,int makh,int maloaikh,int makhuvuc,string tenkh,string tencuahang,string diachi,string dienthoai,int tinhtrangkh,string sanpham, string ghichu_kh,int stt)
        {
            return da.MyExecuteNonQuery("Pro_Insert_DMKhachHang", CommandType.StoredProcedure, ref err, new SqlParameter("@makh", makh),new SqlParameter("@maloaikh", maloaikh),  new SqlParameter("@makv", makhuvuc), new SqlParameter("@tenkh", tenkh), new SqlParameter("@tench", tencuahang), new SqlParameter("@diachi", diachi), new SqlParameter("@dienthoai", dienthoai), new SqlParameter("@tinhtrang", tinhtrangkh),new SqlParameter("@sanpham",sanpham), new SqlParameter("@ghichu", ghichu_kh),new SqlParameter("stt",stt));
        }
        public bool deleteDMKhachHang(ref string err, int Makh)
        {
            return da.MyExecuteNonQuery("Pro_Delete_DMKhachHang", CommandType.StoredProcedure, ref err, new SqlParameter("@makh",Makh));
        }
        public bool updateDMKhachHang(ref string err, int makh, int maloaikh, int makhuvuc, string tenkh, string tencuahang, string diachi, string dienthoai, int tinhtrangkh,string sanpham, string ghichu_kh,int stt)
        {
            return da.MyExecuteNonQuery("Pro_Update_DMKhachHang", CommandType.StoredProcedure, ref err, new SqlParameter("@makh", makh), new SqlParameter("@maloaikh", maloaikh), new SqlParameter("@makv", makhuvuc), new SqlParameter("@tenkh", tenkh), new SqlParameter("@tench", tencuahang), new SqlParameter("@diachi", diachi), new SqlParameter("@dienthoai", dienthoai), new SqlParameter("@tinhtrang", tinhtrangkh), new SqlParameter("@sanpham", sanpham), new SqlParameter("@ghichu", ghichu_kh), new SqlParameter("@stt", stt));
        }
        public bool updateTTKhachHang(ref string err, int makh,int tinhtrangkh)
        {
            return da.MyExecuteNonQuery("Pro_Update_ttkh", CommandType.StoredProcedure, ref err, new SqlParameter("@makh", makh),new SqlParameter("@tinhtrang", tinhtrangkh));
        }
        public object layma(ref string err, string str)
        {
            return da.lay(str, CommandType.Text, ref err);
        }
        public void fillCombo(string sql, ComboBox cmb, string text, string value)
        {
            DataSet ds = new DataSet();
            ds=da.ExectueQueryDataset(sql,CommandType.Text,null);
        
            cmb.DataSource = ds.Tables[0];
            cmb.DisplayMember = text;
            cmb.ValueMember = value;
        }
        public void fillcheckedListBox(string sql, CheckedListBox chkli, string text, string value)
        {
            DataSet ds = new DataSet();
            ds = da.ExectueQueryDataset(sql, CommandType.Text, null);
            chkli.DataSource = ds.Tables[0];
            chkli.DisplayMember = text;
            chkli.ValueMember = value;
        }
        //thêm yêu cầu để: xét quà biếu
        public bool insert_Kh_duoc_dt_default(ref string err, 
            int madt, 
            int makh, 
            DateTime ngaydautu, 
            double tongdautu, 
            string ghichu,int soluongdt,int dongiadt,DateTime thoihandt,int trangthai)
        {
            return da.MyExecuteNonQuery("Pro_Insert_Duocdt", 
                CommandType.StoredProcedure, ref err, 
                new SqlParameter("@madautu", madt), 
                new SqlParameter("@makh", makh), 
                new SqlParameter("@ngaydt", ngaydautu),
                new SqlParameter("@tongdautu", tongdautu), 
                new SqlParameter("@ghichu", ghichu),
                new SqlParameter("@SOLUONGDAUTU",soluongdt),
                new SqlParameter("@DONGIA_DAUTU_SP",dongiadt),
                new SqlParameter("@THOIHAN_DAUTU_SP",ngaydautu),
                new SqlParameter("@TRANGTHAI_KH_D_DT",trangthai));
        }
        /// <summary>
        /// insert vao bảng khách hàng mua sản phẩm
        /// </summary>
        /// <param name="err">thông tin lỗi</param>
        /// <param name="masp">masp</param>
        /// <param name="makh"></param>
        /// <param name="ngaydf"></param>
        /// <param name="manv"></param>
        /// <param name="soluongmua"></param>
        /// <param name="giaspmua"></param>
        /// <param name="giachietkhaumua"></param>
        /// <param name="thanhtien"></param>
        /// <param name="ghichu"></param>
        /// <returns></returns>
        public bool insert_kh_mua_sp_default(ref string err,
            int masp,
            int makh, 
            DateTime ngaydf, 
            int manv,
            int soluongmua,
            double giaspmua,
            double giachietkhaumua,
            double thanhtien,
            string ghichu)
        {
            return da.MyExecuteNonQuery("Pro_Insert_Duocdt",
                CommandType.StoredProcedure, ref err,
                new SqlParameter("@masp", masp),
                new SqlParameter("@makh", makh),
                new SqlParameter("@ngaymua", ngaydf),
                new SqlParameter("@manv",manv),
                new SqlParameter("@soluongmua", soluongmua),
                new SqlParameter("@GIA_SP_NGAYMUA", giaspmua),
                new SqlParameter("@GIA_CHIETKHAU_NGAYMUA",giachietkhaumua),
                new SqlParameter("@THANHTIEN", thanhtien),
                new SqlParameter("@GHICHU_KHMSP", ghichu));
        }
        public bool insert_kh_mua_sp_default(int makh, DateTime ngaydf, int manv)
        {
           // string sql = string.Format("Insert into KH_Mua_SP(masp,makh,ngaymua,manhanvien) values(1,{0},'{1})',1)",makh,ngaydf);
            string sql =string.Format("INSERT INTO [QuanLyBanHang].[dbo].[KH_MUA_SP]([MASP],[MAKH],[NGAYMUA],[MANHANVIEN],[SOLUONGMUA],[GIA_SP_NGAYMUA],[GIA_CHIETKHAU_NGAYMUA],[THANHTIEN],[GHICHU_KHMSP]) VALUES (1,{0},'{1}',{2},0,0,0,0,'default')",makh,ngaydf,manv);
            return da.ExecuteQuery(sql);
           
        }
        //nap chong
    //    
        public bool insert_khachhang_thanhtoan_default(ref string err,
            int makh,
            DateTime ngaydf,
            double sotienthanhtoan,
            int nhanvien_nhantien,
            string ghichu
            )
        {
            return da.MyExecuteNonQuery("insert_kh__thanhtoan_default",
                CommandType.StoredProcedure, ref err,
                new SqlParameter("@makh", makh),
                new SqlParameter("@ngaythanhtoan", ngaydf),
                new SqlParameter("@sotienthanhtoan", sotienthanhtoan),
                new SqlParameter("@nhanvien_Nhantien", nhanvien_nhantien),
                new SqlParameter("@ghichukhtt", ghichu));
        }
        //------------------------------------
        public bool insert_khachhang_thanhtoan_default(int makh, DateTime ngaydf, int manv)
        {
            // string sql = string.Format("Insert into KH_Mua_SP(masp,makh,ngaymua,manhanvien) values(1,{0},'{1})',1)",makh,ngaydf);
            string sql = string.Format("INSERT INTO [QuanLyBanHang].[dbo].[KHACHHANG_THANHTOAN]([MaKH],[NgayThanhToan],[SoTienThanhToan],[NhanVien_NhanTien],[GhiChuKHTT]) VALUES ({0},'{1}',0,{2},'default')", makh, ngaydf, manv);

            return da.ExecuteQuery(sql);

        }

        //public bool insert_kh_mua_sp_default(int makh, DateTime ngaydefault)
        //{
            
            
        //        String sql = String.Format("Insert into KH_Mua_SP(MaSP, MaKH, NgayMua, MaNhanVien, SoLuongMua, Gia_SP_NgayMua, Gia_ChietKhau_NgayMua, Ghichu_KHMSP, ThanhTien)"
        //            + " Values('1', '{0}', '{1}', '1', '0', '0', '0', 'default', '0')", makh , ngaydefault);
        //        if (da.ExecuteQuery(sql))
        //        {
        //            return true;
        //        }

           


        //}
    }
}

